package com.tyros.common.config;

import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * swagger 配置类，使用 Knife4j3
 *
 * @author zhangshuaiyin
 * @date 2022/1/27 22:01
 */
@Slf4j
@EnableOpenApi
@Configuration
@ConditionalOnClass(WebMvcConfigurer.class)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@EnableConfigurationProperties(SwaggerConfig.SwaggerProperties.class)
public class SwaggerConfig {

    private final SwaggerProperties properties;

    public SwaggerConfig(SwaggerProperties properties) {
        this.properties = properties;
    }

    /**
     * 构建api
     *
     * @return Docket
     */
    @Bean
    @ConditionalOnProperty(name = "tyros.swagger.enable", havingValue = "true")
    public Docket createRestApi(SwaggerProperties properties) {
        return new Docket(DocumentationType.OAS_30)
                .enable(properties.enable)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 生成文档摘要信息,如标题、描述、作者、版本号等
     *
     * @return ApiInfo
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(properties.title)
                .description(properties.description)
                .contact(new Contact(properties.author, properties.url, properties.email))
                .version(properties.version)
                .build();
    }

    @Data
    @ConfigurationProperties(prefix = "tyros.swagger")
    public static class SwaggerProperties {
        private boolean enable = true;
        private String title = "Tyros 接口文档";
        private String description = "Tyros Application Swagger Documentation";
        private String author = "zhangshuaiyin";
        private String url = "https://yuque.com/zhangshuaiyin";
        private String email = "594983498@qq.com";
        private String version = "1.0.0";
    }
}
